#!/bin/bash

# 定义日志文件夹和日志文件
log_folder="log"
log_file="usb.log"

# 确保日志文件夹存在
mkdir -p "$log_folder"

# 获取当前时间的函数
get_current_time() {
    date '+%Y-%m-%d %H:%M:%S'
}

usb_test()
{	echo "$1 write rate:"
	sudo dd if=/dev/zero of=/dev/$1 bs=4M count=16 conv=fdatasync
	sleep 0.1
	sudo sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"
	sleep 0.1
	echo "$1 read rate:"
	sudo dd if=/dev/$1 of=/dev/null bs=4M count=16
}

while [ 1 ]
do
	if lsblk | grep -q sda; then
		usb_test sda
		echo "$(get_current_time) - sda exists, usb_test returned $result" | tee -a  "$log_folder/$log_file"
	else
        echo "$(get_current_time) - sda does not exist" | tee -a  "$log_folder/$log_file"
	fi
	
	if lsblk | grep -q sdb; then
		usb_test sdb
        echo "$(get_current_time) - sdb exists, usb_test returned $result" | tee -a  "$log_folder/$log_file"
	else
		echo "$(get_current_time) - sdb does not exist" | tee -a  "$log_folder/$log_file"
	fi

	sleep 2
done
